package com.lfm.jdbc.utils;

import com.lfm.jdbc.datasource.Actor;
import org.junit.jupiter.api.Test;

import java.sql.*;
import java.util.ArrayList;

public class JDBCUtilsByDruid_USE {
    @Test
    public void testSelect() {

        System.out.println("测试druid方式");
        //1. 得到连接
        Connection connection = null;
        //2. 组织SQL
        String sql = "select * from actor where id >= ?";
        PreparedStatement preparedStatement = null;
        ResultSet set = null;
        ArrayList<Actor> list = new ArrayList<>();
        //3. 创建
        try {
            connection = JDBCUtilsByDruid.getConnection();
            System.out.println(connection.getClass());
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 1);
            //执行
            set = preparedStatement.executeQuery();
            //遍历
            while (set.next()) {
                String id = set.getString("id");
                String name = set.getString("name");
                String sex = set.getString("sex");
                Date date = set.getDate("borndate");
                String phone = set.getString("phone");
                System.out.println(id + "\t" + name + "\t" + sex + "\t" + date + "\t" + phone+ "\t");
                //把得到的resultset的记录 封装到Actor对象,放入list集合
                list.add(new Actor(id, name, sex, date, phone));
            }
            System.out.println("list集合数据=" + list);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源
            JDBCUtilsByDruid.close(null, preparedStatement, connection);
        }

    }
}
